//
//  BaseTabBarController.m
//  IM
//
//  Created by limaohuyu on 2020/7/3.
//  Copyright © 2020 czw. All rights reserved.
//

#import "BaseTabBarController.h"
#import "BaseNavigationController.h"
#import "HomeMainViewController.h"
#import "AddressBookViewController.h"
#import "MyMainViewController.h"
#import "AppDelegate.h"

@interface BaseTabBarController ()<AxcAE_TabBarDelegate>
@property (nonatomic,strong) NSMutableArray  * tabBarConfs ;
@end

@implementation BaseTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBarConfs = [[NSMutableArray alloc]init];
    [self setDefaultTabBar];
    
}
#pragma mark == 设置中彩默认TabBar
- (void)setDefaultTabBar{
    
    NSMutableArray *myViewControllers  = [[NSMutableArray alloc]init];
    
    BaseNavigationController * hemeNav  = [self setNewNavRootVC:[HomeMainViewController new] withTabBarItemImageNmae:@"icon_sy_sy" tabBarItemSelectedImageNmae:@"icon_sy_sy_xa" tabBarItemTitle:@"首页"];
    [myViewControllers addObject:hemeNav];
    
        BaseNavigationController *  addressBookNav  = [self setNewNavRootVC:[AddressBookViewController new] withTabBarItemImageNmae:@"icon_sy_yc" tabBarItemSelectedImageNmae:@"icon_sy_yc_xa" tabBarItemTitle:@"通讯录"];
    [myViewControllers addObject:addressBookNav];
    
    BaseNavigationController *  personalCenterNav  = [self setNewNavRootVC:[MyMainViewController new] withTabBarItemImageNmae:@"icon_sy_wd" tabBarItemSelectedImageNmae:@"icon_sy_wd_xa" tabBarItemTitle:@"我的"];
    [myViewControllers addObject:personalCenterNav];
    self.viewControllers = myViewControllers;
    
    [self creatAxcTabBar];
    
    
}
#pragma mark == creatAxcTabBar
- (void)creatAxcTabBar{
    if (self.axcTabBar) {
        [self.axcTabBar removeFromSuperview];
    }
    self.axcTabBar = [AxcAE_TabBar new] ;
    self.axcTabBar.tabBarConfig = self.tabBarConfs;
    // 7.设置委托
    self.axcTabBar.delegate = self;
    
    [self.tabBar addSubview: self.axcTabBar];
    [self.tabBar bringSubviewToFront:self.axcTabBar];
    [self addLayoutTabBar]; 
    
    
}
// 画背景的方法，返回 Tabbar的背景
- (UIImageView *)drawTabbarBgImageView
{
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, -20,keyScreenWidth , SRARUSBAR_TABAR_HEIGHT +20)];// ScreenW设备的宽
    imageView.image = [UIImage imageNamed:@"tabBarBackgroundImage"];
    return imageView;
}


//现在的创建NavigationController
- (BaseNavigationController *)setNewNavRootVC:(UIViewController *)viewController withTabBarItemImageNmae:(NSString *)imageName tabBarItemSelectedImageNmae:(NSString *)selectedImageName tabBarItemTitle:(NSString *)tabBarItemTitle{
    
    // 1.根据集合来创建TabBar构造器
    AxcAE_TabBarConfigModel *model = [AxcAE_TabBarConfigModel new];
    // 2.item基础数据三连
    model.itemTitle =tabBarItemTitle;
    model.selectImageName = selectedImageName;
    model.normalImageName = imageName;
    model.interactionEffectStyle = AxcAE_TabBarInteractionEffectStyleSpring;
    // 3.设置单个选中item标题状态下的颜色
    model.selectColor = UIColorFromRGB(0xE9453B);
    model.normalColor = UIColorFromRGB(0xEDDEE6);
    [self.tabBarConfs addObject:model];
    BaseNavigationController * baseNav = [[BaseNavigationController alloc] initWithRootViewController:viewController];
    return baseNav;
}
//  添加适配
- (void)addLayoutTabBar{
    
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    
    self.axcTabBar.frame = self.tabBar.bounds;
    [self.axcTabBar viewDidLayoutItems];
}
#pragma mark ==  AxcAE_TabBarDelegate 实现代理，如下：
- (void)axcAE_TabBar:(AxcAE_TabBar *)tabbar selectIndex:(NSInteger)index{
    [self setSelectedIndex:index];
}
- (void)setSelectedIndex:(NSUInteger)selectedIndex{
    [super setSelectedIndex:selectedIndex];
    if(self.axcTabBar.tabBarItems.count >0){
        self.axcTabBar.selectIndex = selectedIndex;
    }
}
@end
